package com.cheetah.mapper;

import com.cheetah.entity.User;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

public interface UserMapper {
    @Delete({
        "delete from user",
        "where id = #{id,jdbcType=INTEGER}"
    })
    int deleteByPrimaryKey(String id);

    @Insert({
        "insert into user (name, sex, ",
        "age, account, password,openId)",
        "values (#{name,jdbcType=VARCHAR}, #{sex,jdbcType=CHAR}, ",
        "#{age,jdbcType=INTEGER}, #{account,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR},#{openId,jdbcType=VARCHAR})"
    })
    @SelectKey(statement="SELECT LAST_INSERT_ID()", keyProperty="id", before=false, resultType=Integer.class)
    int insert(User record);

    @Select({
        "select",
        "id, name, sex, age, account, password",
        "from user",
        "where id = #{id,jdbcType=VARCHAR}"
    })
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
        @Result(column="sex", property="sex", jdbcType=JdbcType.CHAR),
        @Result(column="age", property="age", jdbcType=JdbcType.INTEGER),
        @Result(column="account", property="account", jdbcType=JdbcType.VARCHAR),
        @Result(column="password", property="password", jdbcType=JdbcType.VARCHAR)
    })
    User selectByPrimaryKey(String id);

    @Select({
        "select",
        "id, name, sex, age, account, password",
        "from user"
    })
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
        @Result(column="sex", property="sex", jdbcType=JdbcType.CHAR),
        @Result(column="age", property="age", jdbcType=JdbcType.INTEGER),
        @Result(column="account", property="account", jdbcType=JdbcType.VARCHAR),
        @Result(column="password", property="password", jdbcType=JdbcType.VARCHAR)
    })
    List<User> selectAll();

    @Update({
        "update user",
        "set name = #{name,jdbcType=VARCHAR},",
          "sex = #{sex,jdbcType=CHAR},",
          "age = #{age,jdbcType=INTEGER},",
          "account = #{account,jdbcType=VARCHAR},",
          "password = #{password,jdbcType=VARCHAR}",
        "where id = #{id,jdbcType=VARCHAR}"
    })
    int updateByPrimaryKey(User record);

    @Select({
            "select",
            "id, name, sex, age, account",
            "from user where account=#{account,jdbcType=VARCHAR}"
    })
    User findByAccount(String account);

    @Select({
            "select",
            "id",
            "from user where parent=#{parent,jdbcType=INTEGER}"
    })
    User selectOneByParentId(Integer parent);

    @Select({
            "select",
            "*",
            "from user where id in (#{ids})"
    })
    List<User> selectByIds(String ids);
}